<?php
require_once("../includes/config.inc.php");
validate_admin();
$id = $_REQUEST['id'];
$subjectId = $_REQUEST['subjectId'];

if($_POST)
{

	@extract($_POST);
	// Edit Code
	
	$noerror=true;
	if(isFieldDuplicate($tableName='tbl_exam',$fieldName='name',$filedValue=$name,$autoIdName='id',$autoIdValue=$id,$isNullAllowed=false)) 
	{	
		$_SESSION['sessMsg'] = " Sorry, this Exam ".$name." already exists. Please choose a different Exam Name. ";
		$noerror=false;
	}
	if($noerror)
	{	
		if($id)
		{
			$sqlEdit = " update tbl_exam set
			name = '$name',
			subjectId = '$subjectId',
			description = '$description',
			time = '$time',
			date = '$date'
			where id ='$id'";
			executeQuery($sqlEdit);

			$_SESSION['sessMsg'] = "Detail Edited Successfully. ";
			header("location: onlineExamManager.php?subjectId=$subjectId");
			exit();
		}
		else
		{
			// Add Code
			$sqlPA = " insert into tbl_exam 
			(
				name,
				subjectId,
				description,
				date,	
				time,
				status
			)
			values
			(
				'$name',
				'$subjectId',
				'$description',
				'$date',
				'$time',
				'1'
			)
			";
			executeQuery($sqlPA );	

			$_SESSION['sessMsg'] = " New Record Added Successfully. ";
			header("location: onlineExamManager.php?subjectId=$subjectId");
			exit();
		}
	}		
}
			
if($id)
{
	$sqlPD = " select * from tbl_exam where id = '$id' ";
	$resPD = executeQuery($sqlPD);
	if($rowPD = mysql_fetch_array($resPD))
	{	
		@extract($rowPD);
		if($subjectId){
			$sqlCN = " select courseId from tbl_subjects where id = '$subjectId' ";
			$courseId = getSingleResult($sqlCN);
		}
	}
	
	$sqlA = "select * from tbl_answers where questionId = '$id' ";
	$resA = executeQuery($sqlA);
}
?>